#include <windows.h>
#include <string.h>
#include <stdio.h>

int wmain(int argc, wchar_t *argv[])
{
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind = INVALID_HANDLE_VALUE;
	wchar_t DirSpec[MAX_PATH+1];  // directory specification
	DWORD dwError;

	if (argc > 1)
	{
		wcsncpy_s(DirSpec, MAX_PATH, argv[1], wcslen(argv[1])+1);
		wcsncat_s(DirSpec, MAX_PATH, L"\\*", 3);
	}
	else
	{
		wcscpy_s(DirSpec, MAX_PATH, L"*");
	}

	hFind = FindFirstFile(DirSpec, &FindFileData);

	if (hFind == INVALID_HANDLE_VALUE) 
	{
		wprintf(L"Invalid file handle. Error is %u\n", GetLastError());
		return(-1);
	} 
	else 
	{
		int count = 0;
		DWORD size = 0;
		while (FindNextFile(hFind, &FindFileData) != 0) 
		{
			count++;
			size += FindFileData.nFileSizeLow;
			if (count % 1000 == 0)
			{
				wprintf(L"%d files, %d Mb\n", count, size/1024/1024);
			}
		}
		wprintf(L"%d files, %d Mb\n", count, size/1024/1024);
		
		dwError = GetLastError();
		FindClose(hFind);
		if (dwError != 0 && dwError != ERROR_NO_MORE_FILES) 
		{
			wprintf(L"FindNextFile error. Error is %u\n", dwError);
			return(-1);
		}
	}
	return (0);
}
